package com.ruoyi.common.utils.mobilecode;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 *  短信服务
 */
@Service
public class MsgCodeService {
    /**
     * 发短信
     * @param phoneNum
     * @param code
     * @return
     */
    public Boolean sendMsg(String phoneNum,String code){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou"
                , "LTAI4G9quN9jPg8u81QXCDgM", "Tzq2N3R3eVuuGOgnIVDA57aMkOZSqf");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNum);
        request.putQueryParameter("SignName", "微省宝");
        request.putQueryParameter("TemplateCode", "SMS_204865155");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println("短信发送code = "+code+"返回值 = "+response.getData());
            JSONObject json = JSONObject.parseObject(response.getData());
            if(json.get("Code").equals("OK")){
                return true;
            }
            return false;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

}
